import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:sinosheets/components/NetWorkImage/NetworkImageWidget.dart';
import 'package:sinosheets/main_config.dart';
import 'package:sinosheets/route/route_utils.dart';

import 'logic.dart';

class FreeGiftsPage extends StatefulWidget {
  const FreeGiftsPage({Key? key}) : super(key: key);

  @override
  State<FreeGiftsPage> createState() => _FreeGiftsPageState();
}

class _FreeGiftsPageState extends State<FreeGiftsPage> {
  final FreeGiftsLogic logic = Get.put(FreeGiftsLogic());

  @override
  Widget build(BuildContext context) {
    return GetBuilder<FreeGiftsLogic>(builder: (get) {
      return Scaffold(
        appBar: AppBar(
          title: Text('Free Gifts', style: TextStyle(fontSize: 20.r, fontWeight: FontWeight.bold)),
          centerTitle: true,
        ),
        //紧跟listview的底部加一个更多按钮
        body: ListView.builder(
          itemCount: get.zeroAndDiscountList.length + 1,
          itemBuilder: (context, index) {
            if (index == get.zeroAndDiscountList.length) {
              return GestureDetector(
                onTap: () {
                  RouteUtils.toWebView(url: AppPrefix.seagoUrl);
                },
                child: Container(
                  height: 50.r,
                  width: 100.r,
                  margin: EdgeInsets.symmetric(horizontal: 10.r, vertical: 5.r),
                  decoration: BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.circular(10.r),
                    border: Border.all(color: Colors.grey.withAlpha(50)),
                  ),
                  child: Center(
                    child: Text('More', style: TextStyle(fontSize: 16.r, fontWeight: FontWeight.bold, color: Colors.grey)),
                  ),
                ),
              );
            }
            return _buildFreeGiftItem(get.zeroAndDiscountList[index]);
          },
        ),
      );
    });
  }

  Widget _buildFreeGiftItem(Map<String, dynamic> item) {
    Map mallProduct = item['mallProduct'] ?? {};
    String symbol = mallProduct['symbol'] ?? '';
    return GestureDetector(
      onTap: () {
        RouteUtils.toProductDetail(mallProduct, fromFreeGifts: true);
      },
      child: Container(
        height: 110.r,
        padding: EdgeInsets.symmetric(horizontal: 10.r, vertical: 5.r),
        margin: EdgeInsets.symmetric(horizontal: 10.r, vertical: 5.r),
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(10.r),
          border: Border.all(color: Colors.grey.withAlpha(50)),
        ),
        child: Row(
          children: [
            NetworkImageWidget(
              url: mallProduct['img'] ?? '',
              width: 100.r,
              height: 100.r,
              fit: BoxFit.cover,
            ),
            SizedBox(width: 10.r),
            Expanded(
                child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  mallProduct['productName'] ?? '',
                  style: TextStyle(fontSize: 16.r, fontWeight: FontWeight.bold),
                  maxLines: 2,
                  overflow: TextOverflow.ellipsis,
                ),
                Spacer(),
                Row(
                  children: [
                    Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Text("${mallProduct['price']} $symbol", style: TextStyle(fontSize: 16.r, fontWeight: FontWeight.bold, color: Colors.red)),
                        Text("${mallProduct['crossedPrice']} $symbol", style: TextStyle(fontSize: 12.r, color: Colors.grey, decoration: TextDecoration.lineThrough)),
                      ],
                    ),
                    Spacer(),

                    ///复制inId
                    GestureDetector(
                        onTap: () {
                          Clipboard.setData(ClipboardData(text: mallProduct['inId'] ?? ''));
                        },
                        child: Row(
                          children: [
                            Text("${mallProduct['inId']}", style: TextStyle(fontSize: 12.r, color: Colors.grey)),
                            Icon(Icons.copy, color: Colors.grey, size: 16.r),
                          ],
                        ))
                  ],
                ),
              ],
            )),
          ],
        ),
      ),
    );
  }

  @override
  void dispose() {
    Get.delete<FreeGiftsLogic>();
    super.dispose();
  }
}
